#region Summary

// 
// Owner : JackieHan 
// 
// Solution : WebShopApi 
// 
// Project : Jackie.Utility
// 
// File : FileItem.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 自定义文件包装类
// 
// History : 
//			2011-04-14 22:31:29	    Create 
// 

#endregion

#region Using

using System;
using System.IO;

#endregion

namespace Jackie.Utility.Web
{
    /// <summary> 
    /// 自定义文件包装类
    /// </summary>
    public class FileItem
    {
        private readonly FileInfo fileInfo;
        private byte[] content;
        private string fileName;
        private string mimeType;

        #region 构造器

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="fileInfo">本地文件</param>
        public FileItem(FileInfo fileInfo)
        {
            if (fileInfo == null)
                throw new ArgumentNullException("fileInfo");
            if (!fileInfo.Exists)
                throw new FileNotFoundException("file is not found!", fileInfo.FullName);
            this.fileInfo = fileInfo;
            if (fileInfo.Extension.ToUpper().Equals(".XML"))
                mimeType = "text/xml";
        }

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="filePath">本地文件全路径</param>
        public FileItem(string filePath)
            : this(new FileInfo(filePath))
        {
        }

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="fileName">文件名（服务端持久化字节流到磁盘时的文件名）</param>
        /// <param name="content">文件字节流</param>
        public FileItem(string fileName, byte[] content)
        {
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");
            if (content == null || content.Length == 0)
                throw new ArgumentNullException("content");

            this.fileName = fileName;
            this.content = content;
        }

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="fileName">文件名（服务端持久化字节流到磁盘时的文件名）</param>
        /// <param name="content">文件字节流</param>
        /// <param name="mimeType">媒体类型</param>
        public FileItem(string fileName, byte[] content, string mimeType)
            : this(fileName, content)
        {
            if (string.IsNullOrEmpty(mimeType))
                throw new ArgumentNullException("mimeType");
            this.mimeType = mimeType;
        }

        #endregion

        /// <summary>
        /// 获取文件名
        /// </summary>
        /// <returns></returns>
        public string GetFileName()
        {
            if (fileName == null && fileInfo != null && fileInfo.Exists)
            {
                fileName = fileInfo.FullName;
            }
            return fileName;
        }

        /// <summary>
        /// 获取文件扩展名
        /// </summary>
        /// <returns></returns>
        public string GetMimeType()
        {
            return mimeType ?? (mimeType = GetMimeType(GetContent()));
        }

        /// <summary>
        /// 获取文件字节流
        /// </summary>
        /// <returns></returns>
        public byte[] GetContent()
        {
            if (content == null && fileInfo != null && fileInfo.Exists)
            {
                using (FileStream fileStream = fileInfo.OpenRead())
                {
                    content = new byte[fileStream.Length];
                    fileStream.Read(content, 0, content.Length);
                }
            }
            return content;
        }

        /// <summary>
        /// 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
        /// </summary>
        /// <param name="fileData">文件字节流</param>
        /// <returns>JPG, GIF, PNG or null</returns>
        private string GetMimeType(byte[] fileData)
        {
            if (fileData == null || fileData.Length < 10)
                return null;
            if (fileData[0] == 'G' && fileData[1] == 'I' && fileData[2] == 'F')
                return "GIF";
            if (fileData[1] == 'P' && fileData[2] == 'N' && fileData[3] == 'G')
                return "PNG";
            if (fileData[6] == 'J' && fileData[7] == 'F' && fileData[8] == 'I' && fileData[9] == 'F')
                return "JPG";
            if (fileData[0] == 'B' && fileData[1] == 'M')
                return "BMP";
            return null;
        }
    }
}